
<?php

/*
class GeoController
{
    
}

 * 
 */


if ( ! class_exists( 'GeoMashupHttpGeocoder' ) ) {
/**
 * The Geo Mashup Http Geocoder base class.
 *
 * @since 1.4
 */
abstract class GeoMashupHttpGeocoder {
	/**
	 * WP_Http instance
	 * @since 1.4
	 * @var float
	 */
	protected $http;
	/**
	 * Parameters to use with http requests.
	 * @var array 
	 */
	public $request_params;
	/**
	 * Language code.
	 * @since 1.4
	 * @var string
	 */
	public $language;
	/**
	 * Maximum number of results
	 * @var int 
	 */
	public $max_results;

	/**
	 * Constructor
	 *
	 * @param string $args Optional array of arguments:
	 * 		language - two digit language code, defaults to blog language
	 * 		max_results - maximum number of results to fetch
	 * 		default http params - array of WP_Http request parameters, including timeout
	 */
	public function  __construct( $args = array() ) {
		$defaults = array(
			'language' => get_locale(),
			'max_results' => 10,
			'request_params' => array( 'timeout' => 3.0 )
		);
		$args = wp_parse_args( $args, $defaults );
		extract( $args );
		$this->language = GeoMashupDB::primary_language_code( $language );
		$this->max_results = absint( $max_results );
		$this->request_params = $request_params;
		if( !class_exists( 'WP_Http' ) )
			include_once( ABSPATH . WPINC. '/class-http.php' );
		$this->http = new WP_Http();
	}

	/**
	 * Look up locations from a text query, like an address or place name.
	 *
	 * @since 1.4
	 *
	 * @param string $query Text query.
	 * @return array|WP_Error Array of search result locations.
	 */
	abstract public function geocode( $query );

	/**
	 * Look up additional location information from coordinates.
	 *
	 * Resulting locations have as much address information as
	 * possible filled in. Most services return the nearest match
	 * first, and some return only one.
	 *
	 * @since 1.4
	 *
	 * @param float $lat Latitude
	 * @param float $lng Longitude
	 * @return array|WP_Error Array of search result locations.
	 */
	abstract public function reverse_geocode( $lat, $lng );
}

/**
 * HTTP geocoder using the geonames web services.
 *
 * Includes an additional method for looking up administrative area names.
 *
 * @since 1.4
 */
class GeoMashupGeonamesGeocoder extends GeoMashupHttpGeocoder {
	/**
	 * The application username to include in geonames API requests.
	 * @var string 
	 */
	private $geonames_username;

	public function  __construct( $args = array() ) {
		global $geo_mashup_options;
		parent::__construct( $args );
		$this->geonames_username = $geo_mashup_options->get( 'overall', 'geonames_username' );
	}

	public function geocode( $query ) {
		$url = 'http://api.geonames.org/searchJSON?username=' . $this->geonames_username .
				'&maxRows=' .  $this->max_results . '&q=' .  urlencode( utf8_encode( $query ) ) .
				'&lang=' . $this->language;

		$response = $this->http->get( $url, $this->request_params );
		if ( is_wp_error( $response ) )
			return $response;

		$status = $response['response']['code'];
		if ( '200' != $status ) 
			return new WP_Error( 'geocoder_http_request_failed', $status . ': ' . $response['response']['message'], $response );

		$data = json_decode( $response['body'] );
		if ( empty( $data ) or 0 == $data->totalResultsCount ) 
			return array();

		$locations = array();
		foreach( $data->geonames as $geoname ) {
			$location = GeoMashupDB::blank_location();
			$location->lat = $geoname->lat;
			$location->lng = $geoname->lng;
			if ( !empty( $geoname->countryCode ) )
				$location->country_code = $geoname->countryCode;
			if ( !empty( $geoname->adminCode1) )
				$location->admin_code = $geoname->adminCode1;
			if ( !empty( $geoname->fcode ) and 'PPL' == substr( $geoname->fcode, 0, 3 ) )
				$location->locality_name = $geoname->name;
			$locations[] = $location;
		}

		return $locations;
	}


	
}

}


 
